import time
from utils.convert import header_placeholder, footer_placeholder, add_ad_service


def rj_imglist(input: list | str) -> list:
    """转换rj图片链接列表
    1. 长度大于5时，截取前5。否则用辰星logo图补齐长度
    2. 每一项转换为图片对象
    3. 返回处理结果

    Args:
        input (list|str): 原始图片列表

    Returns:
        list: [{"photo": url},xxxx]
    """

    logo_url = "https://images.rjmart.cn/image/enqu62up/860d4319-09b2-4af2-8cde-3bb94773fdee.jpg"

    img_list = [
        {
            "photo": "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/targetmol/targetmol1.png"
        },
        {
            "photo": "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/targetmol/targetmol2.png"
        },
        {
            "photo": "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/targetmol/targetmol3.png"
        },
        {
            "photo": "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/targetmol/targetmol4.png"
        },
        {
            "photo": "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/targetmol/targetmol5.png"
        },
    ]
    # 处理字符串输入
    if isinstance(input, str):
        return [{"photo": input}] + img_list[:4]

    # 处理列表输入
    # 截取或补齐长度
    input = input[:5]
    if len(input) < 5:
        input.extend([logo_url] * (5 - len(input)))

    # 将url列表转换为图片对象
    return [{"photo": url} for url in input]


def csm_imglist(input: list | str) -> list:
    """转换rj图片链接列表
    1. 长度大于5时，截取前5。否则用辰星logo图补齐长度
    2. 每一项转换为图片对象
    3. 返回处理结果

    Args:
        input (list|str): 原始图片列表

    Returns:
        list: [url,url,url,url,url]
    """

    logo_url = "https://images.rjmart.cn/image/enqu62up/860d4319-09b2-4af2-8cde-3bb94773fdee.jpg"

    img_list = [
        "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/targetmol/targetmol1.png",
        "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/targetmol/targetmol2.png",
        "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/targetmol/targetmol3.png",
        "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/targetmol/targetmol4.png",
        "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/targetmol/targetmol5.png",
    ]
    # 处理字符串输入
    if isinstance(input, str):
        img_list.insert(0, input)
        return img_list[:5]

    # 处理列表输入
    # 截取或补齐长度
    input = input[:5]
    if len(input) < 5:
        input.extend([logo_url] * (5 - len(input)))

    return input


def gen_upload_dict(item: dict) -> dict:
    """生成 rj 多规格商品上传数据

    Args:
        item (dict): 商品基础item 数据
        product (dict): 商品规格价格数据

    Returns:
        dict: 待上传多规格商品数据
    """

    item["brandId"] = 705
    item["categoryId"] = 243
    item["status"] = 3
    # 创建图片列表
    item["photos"] = rj_imglist(item["photos"])
    item["desc"] = (
        header_placeholder() + add_ad_service() + item["desc"] + footer_placeholder()
    )
    # 创建规格列表
    sku_list = []

    deliver_dict = {"咨询客服": -1, "现货": 1, "2-4周": 14, "5日内发货": 105}

    # 创建 sku 列表
    for _item in item["sku"]:
        # _spec_list = _item["spec"].split(" ")

        sku_list.append(
            {
                "code": item["code"] + "-" + _item["spec"],
                "price": round(int(_item["price"]) * 0.75),
                "directoryPrice": 1.9 * _item["price"],
                "stock": 999,
                # "carryFeeTemplateId": "4971",  # 15元邮费
                "carryFeeTemplateId": 4611,  # 15元邮费
                "skuAttributes": [
                    {
                        "attributeName": "产品规格",
                        "attributeValue": _item["spec"],
                    }
                ],
                "deliveryTime": deliver_dict.get(_item["delivery"], -1),
                "unit": "瓶",
            }
        )
    del item["sku"]
    item["skuInfos"] = sku_list
    return item


def csm_delivery(delivery: str) -> int:
    """转换 csm 配送时间"""
    deliver_dict = {"现货": 1, "2-4周": 14, "6-8周": 45, "5日内发货": 5}
    return deliver_dict.get(delivery, 15)


def gen_csm_upload(item: dict) -> dict:
    """生成 csm 商品上传数据

    Args:
        dict (dict): 商品基础item 数据
    """

    # raw_name = item["name"]
    # raw_code = item["code"]

    item["cateid"] = 385  # 分类id
    item["brandid"] = 300150925  # 品牌id
    item["typeid"] = 300004006  #  类型id
    item["isinsale"] = 1  # 销售状态
    item["stockamount"] = 999  # 库存
    item["maker"] = "上海陶术生物科技股份有限公司"
    item["extends"] = [{"key": "300095006", "value": "咨询客服"}]
    item["rid"] = time.time()

    # item["name"] = raw_name + "" + raw_code
    item["code"] = item["code"] + " " + item["name"]
    item["mktprice"] = round(item["sku"][0]["price"] * 1.9, 2)
    item["price"] = round(item["sku"][0]["price"], 2)
    item["spec"] = item["sku"][0]["spec"].strip() + "/瓶"
    item["unit"] = "瓶"
    item["imgs"] = csm_imglist(item["photos"])  # 创建图片上传列表
    item["intro"] = item["desc"]
    item["cascode"] = item["dangerousInfo"]["casNo"]

    item["deliverycycle"] = csm_delivery(item["sku"][0]["delivery"])

    del item["photos"]
    del item["sku"]
    del item["desc"]
    del item["dangerousInfo"]

    return item
